
#include <iostream>

using namespace std;

int giDeepMax = 8;
int arrEightQueensIndexInfo[8] = {0};
int giTotleNubersRightIndex = 0;
int gCount = 0; 


void print()//输出
{
    int i = 0;
    for ( i = 0; i < 8; i++)
    {   
        int inner;
        for (inner = 0; inner < arrEightQueensIndexInfo[i]; inner++)
            cout << "0";
            cout <<"#";
        for (inner = arrEightQueensIndexInfo[i] + 1; inner < 8; inner++)
            cout << "0";
        cout << endl;
       
    }
    cout << " ** lmx ** info " << endl;
    for ( i = 0; i < 8; i++){
 	cout << " " << arrEightQueensIndexInfo[i];
    }
    cout << endl;
     
    cout << "==========================\n";
}


/*   __________________x 
*   |      \ | /
*   |       \|/
*   |        *(iPosInThisDeep, iDeepIndex )
*   |       
*   |      
*   |
*   y
*    
*   check info: y==x+(iDeepIndex-iPosInThisDeep), x==iPosInThisDeep, y==-x+( iDeepIndex + iPosInThisDeep)
*   y = i
*   
*/
int checkPosIsOk( int iDeepIndex, int iPosInThisDeep){
//int checkPosIsOk(int loop, int value){
#if 0
    int index;
    int data;
    for (index = 0; index < loop; index++)
    {
        data = arrEightQueensIndexInfo[index];
        if (value == data)
            return 0;
        if ((index + data) == (loop + value))
            return 0;
        if ((index - data) == (loop - value))
            return 0;
    }
    return 1;
#endif
#if 1
    int  iDataPosInfo = -1;    
    for( int i=iDeepIndex-1; i>=0; i--){
	iDataPosInfo = arrEightQueensIndexInfo[i];
	cout  << " ** lmx **  iDataPosInfo=" << iDataPosInfo << "; iDeepIndex="<<iDeepIndex <<";iPosInThisDeep" <<"; i=" << i << endl; 
    	if( iDataPosInfo == iPosInThisDeep )
		return -1;
	if( i+iDataPosInfo ==  iDeepIndex+iPosInThisDeep)
		return -1;
	if( i-iDataPosInfo ==  iDeepIndex-iPosInThisDeep)
		return -1;

    }
    return 0;
#endif
}



#if 1
void fDfsGetEightQueens( int iDeepIndex ){
    for( int i=0; i<giDeepMax; i++){
	if( 0==checkPosIsOk( iDeepIndex, i ) ){
                arrEightQueensIndexInfo[iDeepIndex] = i;
		if( iDeepIndex==(giDeepMax-1) ){
			cout  << " ** lmx ** fDfsGetEightQueens i = " <<  i <<" ; giDeepMax= " << giDeepMax<<endl;
			giTotleNubersRightIndex++;
                        print();
                        arrEightQueensIndexInfo[iDeepIndex] = 0;
			return;		
		} //if
		fDfsGetEightQueens( iDeepIndex+1 );
		arrEightQueensIndexInfo[iDeepIndex] = 0;
	} // if( 0==checkPosIsOk( iDeepIndex, i )
    }
#endif

#if 0
void fDfsGetEightQueens( int iDeepIndex ){
    int i=0;
    for (i = 0; i < 8; i++)
    {
	if( 0==checkPosIsOk( iDeepIndex, i ) )
	{
		arrEightQueensIndexInfo[iDeepIndex] = i;
		if (7 == iDeepIndex)
		{
			gCount++, print();
			arrEightQueensIndexInfo[iDeepIndex] = 0;
			return;
		}
		fDfsGetEightQueens(iDeepIndex + 1);
		arrEightQueensIndexInfo[iDeepIndex] = 0;
	}
	
    }

#endif

#if 0
void fDfsGetEightQueens(int index){
    int loop;
    for (loop = 0; loop < 8; loop++)
    {
        if ( 0==checkPosIsOk(index, loop))
        {
            arrEightQueensIndexInfo[index] = loop;
            if (7 == index)
            {
                gCount++, print();
                arrEightQueensIndexInfo[index] = 0;
                return;
            }
            fDfsGetEightQueens(index + 1);
            arrEightQueensIndexInfo[index] = 0;
        }
    }
 #endif
}


int main(){
    fDfsGetEightQueens( 0);
    cout  << " ** lmx ** get RightQueensNumbers = " <<  giTotleNubersRightIndex << "; get gCount:" << gCount<< endl;
    return 0;
} 
